//页面加载的时候就进行查询
$(function(){
	initTree();
	query();
});

//查询二次开发客户
function query(pageNow) {
	controllerWaitingBar(true);
	//获取项目上下文路径
	rootPath = $('#rootPath').val();
	//获取要查询的客户类型
	var customerType=$("#customerType").val();
	
	//获取当前页的下一页/上一页
	if(!pageNow) {
		pageNow = 1;
	}else if(pageNow == "nextPage") {
		pageNow = $('#pageNow').val();
		var pageCount = $('#pageCount').html();
		//如果当前页是最后一页 则下一页无效
		if(pageNow == pageCount) {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 + 1;
	}else if(pageNow == "prevPage") {
		pageNow = $('#pageNow').val();
		//如果当前页是第一页 则上一页无效
		if(pageNow == "1") {
			controllerWaitingBar(false);
			return ;
		}
		pageNow = $('#pageNow').val()*1 - 1;
	}else if(pageNow == "lastPage") {
		pageNow = $('#pageCount').html();
	}
	
	var customId = $('#customId').val();
	var customName = $('#customName').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	var empId = $('#emp').val();
	var deptId = $('#deptId').val();
	var regionalId = $('#area').val();
	var isSort = $('#isSort').val();
	var customState = $('#customState').val();
	if(!checkForm()) {
		return ;
	}
	
	var url = rootPath + "/customerManage/queryCustomer";
	$.post(
		url,
		{
			"customId": customId,
			"customName": customName,
			"phone": phone,
			"email": email,
			"empId": empId,
			"deptId": deptId,
			"regionalId": regionalId,
			"customerType": customerType,
			"isSort": isSort,
			"customState": customState,
			"pageNow": pageNow
		},
		function(data) {
			var json = eval(data);
			if(5 == json) {
				location.href = rootPath + "/login/loginPage";
				return ;
			}
			var html = "";
			html += '<tr>';
			html += '<th>选择&nbsp;<input type="checkbox" onclick="selectedBox(this)" /></th>';
	        html += '<th>会员编号</th>';
	        html += '<th>联系人</th>';
	        html += '<th>手机</th>';
	        html += '<th>电子邮箱</th>';
	        html += '<th>已购商品</th>';
	        html += '<th>所属人</th>';
	        html += '<th>流转时间</th>';
	        html += '<th>所属分公司</th>';
	        html += '<th>所属区域</th>';    
	        html += '<th>接收状态</th>';    
	        html += '<th>操作</th>';    
	        html += '</tr>'; 
	        var isResponsible = $('#isResponsible').val();
	        var userId = $('#userId').val();
	        if(json.length == 0) {
				html += '<tr><td colspan="10">没有查到相关数据！</td></tr>';
			}else {
				
				for(var i=0; i<json.length; i++) {
					var customState = json[i].customState;
					html += '<tr>';
					var checkbox = "";
					if(userId == json[i].empId && customState == 0) {
						checkbox ='<td><input type="checkbox" value="'+
						json[i].ecrId +':'+
						json[i].customId +':'+
						json[i].empId +':'+
						json[i].productId +':'+
						'"  /></td>';
					}else {
						checkbox = "<td></td>";
					}
					html += checkbox;
					if(isResponsible == 0 &&  customState == 0) {
						html += '<td><a href="#">' + json[i].customId + '</a></td>';
					}else {
						html += '<td><a href="#" onclick="goCustomerDetails(' + "'" + json[i].customId + "'" + ')">' + json[i].customId + '</a></td>';
					}
					if(isResponsible == 0 && customState == 0) {
						html += '<td></td>';
						html += '<td></td>';
						html += '<td></td>';
						html += '<td></td>';
						html += '<td></td>';
						html += '<td></td>';
					}else{
						html += '<td>' + json[i].customName + '</td>';
						html += '<td>' + json[i].phone + '</td>';
						html += '<td>' + json[i].email + '</td>';
						html += '<td>' + json[i].productName + '</td>';
						html += '<td>' + json[i].empName + '</td>';
						html += '<td>' + json[i].createTimeString + '</td>';
						if(json[i].companyAbb!=null){
							html += '<td>' + json[i].companyAbb + '</td>';
						}else{
							html += '<td>' + '</td>';
						}
						html += '<td>' + json[i].areaName + '</td>';
					}
					
					//只能接收分配给自己的客户（当分配至人员 = 当前登录人 & 该客户未被接收时 才可进行接受拒绝操作）
					var count =0;
					if(userId == json[i].empId && customState == 0) {
						html += '<td><a href="#" onclick="selectedOne(this, 1)">接收</a> / <a href="#" onclick="selectedOne(this,2)">拒绝</a></td>';
						html += '<td></td>';
					}else if(userId != json[i].empId && customState == 0) {
						html += '<td>未接收</td>';
						html += "<td></td>";
						count++;
					}else if(customState != 0){
						html += "<td>已接收</td>";
						count++;
						if(userId == json[i].empId) {
							html += '<td><a href="#" onclick="queryRemark('+"'" + json[i].customId + "'" +')">查看记录</a> &nbsp' +
							'<a href="#" onclick="showAddRemark('+"'" + json[i].customId + "'" +')">添加记录</a></td>';
						}else{
							html += "<td></td>";
						}
					}
					//都没值
					if(count==0){
						html += "<td></td><td></td>";
					}
					html += '</tr>';
				}
			}
			$('#dataList').html(html);
			
			//显示分页
			if(json.length) {
				var pageNow = json[0].pageNow;
				var pageSize = json[0].pageSize;
				var rowCount = json[0].rowCount;
				var pageCount = json[0].pageCount;
				
				$('#pageNow').val(pageNow);
				$('#pageSize').html("1-" + pageSize);
				$('#rowCount').html(rowCount);
				$('#pageIndex').html(pageNow);
				$('#pageCount').html(pageCount);
			}else {
				$('#pageNow').val("1");
				$('#pageSize').html("1-50");
				$('#rowCount').html("0");
				$('#pageIndex').html("1");
				$('#pageCount').html("1");
			}
			controllerWaitingBar(false);
		}
	);
}

//接收客户
function acceptCustomer(customId) {
	$.post(
		rootPath + "/customerManage/accept",
		{
			"customId": customId,
			"customerType": 4
		},
		function(data) {
			alert(data);
			query(4);
		}
	);
	
}

//推送客户